package com.ahearttogo.session;

import com.ahearttogo.binding.MapperMethod;
import com.ahearttogo.binding.MapperProxy;
import com.ahearttogo.executor.Executor;
import lombok.Data;

import java.lang.reflect.Proxy;

/**
 * @author xiejialin
 * @date 2020/4/21 4:22 PM
 */
@Data
public class DefaultSqlSession implements SqlSession{

    private Configuration configuration;
    private Executor executor;

    public Configuration getConfiguration() {
        return configuration;
    }

    public DefaultSqlSession(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    public <T> T getMapper(Class<T> type) {
        return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type},new MapperProxy<>(this,type));
    }
    @Override
    public <T> T selectOne(MapperMethod mapperMethod, Object statement) throws Exception {

        return  executor.query(mapperMethod,statement);
    }
}
